<template>
  <div class="loginBox">
    <h2 class="title">欢迎注册</h2>
    <el-form :model="ruleForm" ref="ruleForm" :rules="rulesone" label-width="80px">
      <el-form-item label="账号" prop="user" class="fffitem">
        <el-input v-model="ruleForm.user" placeholder="请输入内容" minlength="6"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" class="fffitem">
        <el-input placeholder="请输入密码" v-model="ruleForm.password" show-password minlength="6"></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align:center">
      <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
    </div>
    <p class="goregister">
      <router-link tag="span" to="/login">
        去登录
        <i class="el-icon-right"></i>
      </router-link>
    </p>
  </div>
</template>

<style lang="less">
.loginBox {
  width: 100%;
  height: 100%;
  background-image: url("./../../assets/background.jpg");
  background-size: 100% 100%;
}
.title {
  font-size: 0.2rem;
  color: #fff;
  text-align: center;
  padding: 150px 0 20px 0;
}
form {
  width: 90%;
  text-align: center;
}
.fffitem .el-form-item__label {
  color: #fff !important;
}
.goregister {
  text-align: right;
  color: #fff;
  padding: 20px 20px 0 0;
}
</style>

<script>
import { mapActions } from "vuex";
export default {
  data: function() {
    return {
      ruleForm: {
        user: "",
        password: ""
      },
      rulesone: {
        user: [
          { required: true, message: "请输入账号名称", trigger: "blur" },
          { min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    ...mapActions("userStore", ["set_change_userList"]),
    submitForm(formName) {
      var that = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          let that = this;
          let obj = {
            user: that.ruleForm.user,
            password: that.ruleForm.password
          };
          this.set_change_userList(obj);
          this.$message({
            type: "success",
            message: "注册成功，请登录",
            duration: 500
          });
          that.$router.push('/login');
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>